library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.4 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
# install.packages("vtable")
library(vtable)
## Loading required package: kableExtra
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
# install.packages("Hmisc")
library("Hmisc")
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
##
## src, summarize
## The following objects are masked from 'package:base':
##
## format.pval, units
# install.packages("corrplot")
library(corrplot)
## corrplot 0.92 loaded
library(naniar)
library(corrplot)
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
library(htmltools)
df <- read_csv("../../data/final/merged_data.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## .default = col_double(),
## iso = col_character(),
## country_name = col_character(),
## hdi_value = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
adb <- read_csv("../../data/final/adb-members.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## country_name = col_character(),
## iso = col_character(),
## region = col_character(),
## donor = col_double(),
## sids = col_double(),
## ldc = col_double()
## )
output.fig.dir <- "../../output/figures"
output.tab.dir <- "../../output/tables"
SAVE.RESULTS = TRUE
df <- df %>%
rename(migrant_stock=ims_both_sex,
refugee_stock=estimated_refugee_stock_incl_asylum_seekers_both_sexes,
disaster_displacement=disaster_stock_displacementr_raw,
conflict_displacement=conflict_stock_displacement_raw,
climate_change=CCH,
air_quality=AIR,
rule_of_law=`value.Rule of Law: Estimate`,
gov_effectiveness=`value.Government Effectiveness: Estimate`,
corruption_control=`value.Control of Corruption: Estimate`,
state_legit=`P1: State Legitimacy`,
cpa_d_12=D12,
cpa_d_avg=D_avg,
gdp=`GDP per capita (constant 2015 US$)`,
gini=`value.Gini index (World Bank estimate)`) %>%
mutate(conflict_displacement=conflict_displacement/10000,
disaster_displacement=disaster_displacement/10000,
migrant_stock=migrant_stock/10000,
refugee_stock=refugee_stock/10000,
gdp=gdp/1000,
state_legit=10-state_legit,
hdi_value=as.numeric(hdi_value))
## Warning in mask$eval_all_mutate(quo): NAs introduced by coercion
col_names <- c('migrant_stock'='Migrant Stock (10,000s)',
'refugee_stock'='Refugee Stock (10,000s)',
'disaster_displacement'='Internal Displacement Due to Disasters (10,000s)',
'conflict_displacement'='Internal Displacement Due to Conflict (10,000s)',
'climate_change'='Climate Change',
'air_quality'='Air Quality',
'rule_of_law'='Rule of Law',
'gov_effectiveness'='Government Effectiveness',
'corruption_control'='Control of Corruption',
'state_legit'='State Legitimacy',
'cpa_d_12'='CPA: D-12',
'cpa_d_avg'='CPA: Cluster D Average',
'gdp'='GDP Per Capita (1,000s)',
'hdi_value'="HDI",
'gini'="Gini Index")
generic.cols <- c('iso', 'year', 'country_name', 'region', 'donor', 'sids', 'ldc')
outcome.cols <- c('state_legit', 'cpa_d_avg', 'cpa_d_12')
keep <- df %>%
arrange(year, iso) %>%
select(names(col_names)) %>%
mutate(keep = if_any(everything(), ~ !is.na(.))) %>%
pull(keep)
df <- df %>%
arrange(year, iso) %>%
select(iso, year, names(col_names)) %>%
arrange(year) %>%
filter(keep) %>%
left_join(adb, by='iso')
for (reg in unique(df$region)) {
for (outcome in outcome.cols) {
plt <- df %>%
filter(region==reg) %>%
select('iso', 'year', outcome) %>%
drop_na() %>%
ggplot(aes_string(x='year', outcome)) +
geom_line(aes(color=iso)) +
geom_point(aes(color=iso)) +
labs(title=reg, subtitle=paste(col_names[outcome], 'across years')) +
theme_classic()
print(plt)
if (SAVE.RESULTS) {
ggsave(paste(output.fig.dir, '/year_X_', outcome, '_', tolower(str_replace(reg, " " ,"-")), '.png', sep=''))
}
}
}
## Note: Using an external vector in selections is ambiguous.
## ℹ Use `all_of(outcome)` instead of `outcome` to silence this message.
## ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This message is displayed once per session.
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
for (reg in unique(df$region)) {
for (outcome in outcome.cols) {
plt <- df %>%
filter(region==reg & donor==0) %>%
select('iso', 'year', outcome) %>%
drop_na() %>%
ggplot(aes_string(x='year', outcome)) +
geom_line(aes(color=iso)) +
geom_point(aes(color=iso)) +
labs(title=reg, subtitle=paste(col_names[outcome], 'across years')) +
theme_classic()
print(plt)
if (SAVE.RESULTS) {
ggsave(paste(output.fig.dir, '/year_X_', outcome, '_', tolower(str_replace(reg, " " ,"-")), '_recipients', '.png', sep=''))
}
}
}
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
for (status in c('Donors', 'Recipients')) {
for (outcome in outcome.cols) {
plt <- df %>%
mutate(donor=ifelse(donor, 'Donors', 'Recipients')) %>%
filter(donor==status) %>%
select('iso', 'year', outcome) %>%
drop_na() %>%
ggplot(aes_string(x='year', outcome)) +
geom_line(aes(color=iso)) +
geom_point(aes(color=iso)) +
labs(title=status, subtitle=paste(col_names[outcome], 'across years')) +
theme_classic()
print(plt)
if (SAVE.RESULTS) {
ggsave(paste(output.fig.dir, '/year_X_', outcome, '_', tolower(status), '.png', sep=''))
}
}
}
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Note: Using an external vector in selections is ambiguous.
## ℹ Use `all_of(generic.cols)` instead of `generic.cols` to silence this message.
## ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This message is displayed once per session.
## Note: Using an external vector in selections is ambiguous.
## ℹ Use `all_of(col)` instead of `col` to silence this message.
## ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This message is displayed once per session.
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Warning in max(sdf$year): no non-missing arguments to max; returning -Inf
## Warning in min(sdf$year): no non-missing arguments to min; returning Inf
## Warning in max(sdf$year): no non-missing arguments to max; returning -Inf
## Warning in min(sdf$year): no non-missing arguments to min; returning Inf
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Warning: ggrepel: 7 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
## Saving 7 x 5 in image
## Warning: ggrepel: 7 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
## Warning: ggrepel: 8 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
## Saving 7 x 5 in image
## Warning: ggrepel: 8 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
## Saving 7 x 5 in image
https://www.princeton.edu/~otorres/Panel101R.pdf panel_model <- plm(state_leg ~ climate, data = data_set, indec = c(“iso”, “year”), model = “within”) Maya Van Nuys (she/her) to Everyone (1:31 PM) panel data code (updated): panel_model <- plm(state_leg ~ climate, data = data_set, index = c(“iso”, “year”), model = “within”) library(AER) library(plm)
panel_model <- plm(state_leg ~ climate, data = data_set, index = c(“iso”, “year”), model = “within”, effect = “twoways”)
library(AER)
## Loading required package: car
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
## The following object is masked from 'package:purrr':
##
## some
## Loading required package: lmtest
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
library(plm)
##
## Attaching package: 'plm'
## The following objects are masked from 'package:dplyr':
##
## between, lag, lead
# plm(state_legit ~ air_quality + climate_change + hdi_value + gdp, data = df, index = c("iso", "year"), model = "within")
model <- plm(state_legit ~ air_quality + climate_change + hdi_value + gdp, data = df, index = c("iso", "year"), model = "within", effect = "twoways")
## Warning in pdata.frame(data, index): duplicate couples (id-time) in resulting pdata.frame
## to find out which, use, e.g., table(index(your_pdataframe), useNA = "ifany")
summary(model)
## Twoways effects Within Model
##
## Call:
## plm(formula = state_legit ~ air_quality + climate_change + hdi_value +
## gdp, data = df, effect = "twoways", model = "within", index = c("iso",
## "year"))
##
## Unbalanced Panel: n = 38, T = 1-6, N = 177
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -1.2872e+00 -3.4377e-01 -2.0157e-15 2.9639e-01 1.2152e+00
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## air_quality 0.0067652 0.0046772 1.4464 0.1505
## climate_change -0.0035863 0.0038556 -0.9301 0.3540
## hdi_value 8.2149749 6.3619997 1.2913 0.1989
## gdp -0.0218387 0.0477644 -0.4572 0.6483
##
## Total Sum of Squares: 44.579
## Residual Sum of Squares: 43.395
## R-Squared: 0.026561
## Adj. R-Squared: -0.31789
## F-statistic: 0.88679 on 4 and 130 DF, p-value: 0.47388